﻿namespace CYPlayer.ProjectViews
{
    using System;
    using System.Windows;
    using System.Windows.Controls;

    using CYPlayer.ViewModel;

    public partial class PlayerView : UserControl
    {
        private PlaylistWindow _playlistWindow;
        private PlayerWindow _parent;

        private bool isPlay;

        public PlayerView()
        {
            this.InitializeComponent();
            App.PlayerWindowVM = this.DataContext as PlayerWindowVM;
        }

        public PlayerWindow WindowParent
        {
            get
            {
                return this._parent ?? (this._parent = (PlayerWindow)Window.GetWindow(this));
            }
        }

        private void PlwindowBtnClick(object sender, RoutedEventArgs e)
        {
            if (this._playlistWindow == null)
            {
                this._playlistWindow = new PlaylistWindow
                {
                    Left = this.WindowParent.Left + this.WindowParent.Width,
                    Owner = this.WindowParent
                };
                this._playlistWindow.Top = this.WindowParent.Top + (this.Height - this._playlistWindow.Height) / 2;
                this._playlistWindow.Show();
                this._parent.LocationChanged += this.PlayerWindowLocationChanged;
                this._playlistWindow.LocationChanged += this.PlaylistWindowLocationChanged;
                this._playlistWindow.Closed += this.PlwinClosed;
                return;
            }

            if (!this._playlistWindow.IsVisible)
            {
                this._playlistWindow.Show();
            }
            else
            {
                this._playlistWindow.Hide();
            }
        }

        private void PlwinClosed(object sender, EventArgs e)
        {
            this._playlistWindow = null;
        }

        void PlayerWindowLocationChanged(object sender, EventArgs e)
        {
            if (this._playlistWindow != null)
            {
                this._playlistWindow.Left = this.WindowParent.Left + this._parent.Width;
                this._playlistWindow.Top = this.WindowParent.Top + (this.Height - this._playlistWindow.Height) / 2;
            }
        }

        private void PlaylistWindowLocationChanged(object sender, EventArgs e)
        {
            if (this._playlistWindow.IsActive)
            {
                this.WindowParent.LocationChanged -= this.PlayerWindowLocationChanged;
            }
        }

        private void MLbtnClick(object sender, RoutedEventArgs e)
        {
            this.WindowParent.transitionConrol.ShowPage(
                null,
                typeof(MediaLibView).Name);
        }

        private void PrefBtnClick(object sender, RoutedEventArgs e)
        {
            this.WindowParent.transitionConrol.ShowPage(
                null,
                typeof(PreferencesView).Name);
        }

        private void CmpctModeBtnClick(object sender, RoutedEventArgs e)
        {
            WindowParent.Hide();
            var cmpct = new CompactWindow();
            cmpct.Show();
        }

        private void PlayPause_Click(object sender, RoutedEventArgs e)
        {
            if (!isPlay)
            {
                playIm.Opacity = 0;
                pauseIm.Opacity = 100;
                isPlay = true;
            }
            else
            {
                playIm.Opacity = 100;
                pauseIm.Opacity = 0;
                isPlay = false;
            }
        }
    }
}
